package com.siburian.MyStuff;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Intent;
import android.widget.Toast;
import java.util.*;


public class PlayWithMatemathic extends Activity{
	String jenisOperan ="";
	Bintang dbStar;
	Profil_Stat data_profil;
	TextView angka1;
	TextView angka2;
	TextView operan;
	EditText jwbanAnak;
	Button execute;
	int jumlahSoal = 0;
	int[] firstNumber;
	int[] secondNumber;
	int[] result;
	int[] jawabanAnak;
	int indexData = 0;
	int tempRandom = 0;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_with_mathematic);
        dbStar = new Bintang(this);
        data_profil = new Profil_Stat(this);
        angka1 = (TextView) findViewById(R.id.txtAngka1);
        angka2 = (TextView) findViewById(R.id.txtAngka2);
        operan = (TextView) findViewById(R.id.txtOperan);
        jwbanAnak = (EditText) findViewById(R.id.txtJawabanAnak);
        jumlahSoal = 5;
        Random generator = new Random();
        result = new int[jumlahSoal];
        firstNumber = new int[jumlahSoal];
        secondNumber = new int[jumlahSoal];
        jawabanAnak = new int[jumlahSoal];
        jenisOperan = "kurang";
        for(int i = 0;i < jumlahSoal; i++){
        	tempRandom = generator.nextInt(10);
        	firstNumber[i] = tempRandom;
        	tempRandom = generator.nextInt(10);
        	if(jenisOperan == "kurang"){
        		while(firstNumber[i]<tempRandom){
        			tempRandom = generator.nextInt(10);
        		}
        	}
        	secondNumber[i] = tempRandom;
        }
        for(int i = 0;i < jumlahSoal; i++){
        	if(jenisOperan == "tambah"){
        		result[i] = firstNumber[i] + secondNumber[i];
        	}
        	if(jenisOperan == "kali"){
        		result[i] = firstNumber[i] * secondNumber[i];
        	}
        	if(jenisOperan == "kurang"){
        		result[i] = firstNumber[i] - secondNumber[i];
        	}
        }
        angka1.setText(""+firstNumber[0]);
        angka2.setText(""+secondNumber[0]);
        operan.setText(" + ");
        execute = (Button) findViewById(R.id.btnPeriksaMtk);
        execute.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(jwbanAnak.getText().toString().equals("")){
					Toast.makeText(getBaseContext(), "Silahkan masukkan jawaban terlebih dahulu",
							Toast.LENGTH_LONG).show();
				}
				else{
					jawabanAnak[indexData] = Integer.parseInt(jwbanAnak.getText().toString());
					
					if(indexData == jumlahSoal - 1){
						Toast.makeText(getBaseContext(), "Nilai yang didapat" + periksaJawabanBerhitung(result,jawabanAnak),
								Toast.LENGTH_LONG).show();
						if(dbStar.getJumlah(ListProfil.profileID, ListBab.idBab, ChooseKelas.kelasSiswa, ListBelajar.typeStudy) == 0){
							dbStar.insertDataBintang(ListProfil.profileID, ListBab.idBab, ChooseKelas.kelasSiswa, ListBelajar.typeStudy, periksaJawabanBerhitung(result,jawabanAnak));
							data_profil.addProfile(ListProfil.profileID, ChooseKelas.kelasSiswa, ListBelajar.typeStudy, ListBab.idBab, periksaJawabanBerhitung(result,jawabanAnak));
						}
						if(dbStar.getJumlah(ListProfil.profileID, ListBab.idBab, ChooseKelas.kelasSiswa, ListBelajar.typeStudy) != 0){
							data_profil.addProfile(ListProfil.profileID, ChooseKelas.kelasSiswa, ListBelajar.typeStudy, ListBab.idBab, periksaJawabanBerhitung(result,jawabanAnak));
							if(dbStar.getJumlah(ListProfil.profileID, ListBab.idBab, ChooseKelas.kelasSiswa, ListBelajar.typeStudy) < periksaJawabanBerhitung(result,jawabanAnak)){
								dbStar.updateBintang(ListProfil.profileID, ListBab.idBab, ChooseKelas.kelasSiswa, ListBelajar.typeStudy, periksaJawabanBerhitung(result,jawabanAnak));
							}
						}
						Intent inte = new Intent(PlayWithMatemathic.this,ChooseMenu.class);
						startActivity(inte);
					}
					else{
						if(indexData == jumlahSoal - 2){
							execute.setText("Periksa");
						}
						indexData++;
						jwbanAnak.setText("");
				        angka1.setText(""+firstNumber[indexData]);
				        angka2.setText(""+secondNumber[indexData]);
					}
				}
			}
		});
	}
	public int periksaJawabanBerhitung(int[] jawabanSistem,int[] jawabanSiswa){
		int nilai = 0;
		for(int i = 0; i<jawabanSistem.length;i++){
			if(jawabanSistem[i] == jawabanSiswa[i]){
				nilai = nilai + 10;
			}
	}
	return nilai;
	}
}
